package com.googlecode.icefusion.ui.commons.navigation;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;

import com.googlecode.icefusion.ui.commons.BackingBean;
import com.googlecode.icefusion.ui.commons.constant.ICEfusionConsts;


/**
 * Dynamic menu management using collapsible panels. You get a main menu with entries that have a sub menu.
 * 
 * @author Rainer Eschen
 * 
 */
public class CollapsibleMenu extends BackingBean {

    private static final long serialVersionUID = 4990174959399346975L;

    @Autowired
    private ICEfusionConsts consts;

    /**
     * Menu structure. The class has to handle this as a singleton. If the locale is changed during a session this list
     * has to be reseted, to load corresponding resource bundle entries again.
     */
    ArrayList<SimpleMenuItem> collapsibleMenu = new ArrayList<SimpleMenuItem>();

    /**
     * Defines values for main menu creation: navigationId, label.
     */
    LinkedHashMap<String, String> mainMenu = new LinkedHashMap<String, String>();

    // Add your menu items here

    // ICEfusion standard entries

    /**
     * Defines values for extra menu creation: navigationId, label.
     */
    LinkedHashMap<String, String> extraMenu = new LinkedHashMap<String, String>();

    /**
     * Prepare structure for menu creation.
     */
    protected void init() {

        // Add your menu items here

        // ICEfusion standard entries
        mainMenu.put("mainMenu", consts.getLocalized("application.menu.home", "icefusion"));
        mainMenu.put("extra", consts.getLocalized("application.menu.extra", "icefusion"));

        extraMenu.put("settings", consts.getLocalized("application.menu.extra.settings", "icefusion"));
        extraMenu.put("about", consts.getLocalized("application.menu.extra.about", "icefusion"));
    }

    /**
     * Adds a single entry to the main menu.
     * 
     * @param parent node to add the entry
     * @param entry parameter to create a menu entry
     */
    protected SimpleMenuItem addEntry(List<SimpleMenuItem> parent, Entry<String, String> entry) {

        SimpleMenuItem menuItem = new SimpleMenuItem();
        menuItem.setLabel(entry.getValue());
        menuItem.setAction(entry.getKey());
        parent.add(menuItem);
        return menuItem;
    }

    /**
     * Adds a single entry to any sub menu.
     * 
     * @param parent node to add the entry
     * @param entry parameter to create a menu entry
     */
    protected SimpleMenuItem addEntry(SimpleMenuItem parent, Entry<String, String> entry) {

        SimpleMenuItem menuItem = new SimpleMenuItem();
        menuItem.setLabel(entry.getValue());
        menuItem.setAction(entry.getKey());
        parent.getSubMenu().add(menuItem);
        return menuItem;
    }

    /**
     * Delivers the ICEfaces menu model to render.
     * 
     * @return tree of menu entries
     */
    public List<SimpleMenuItem> getMenuModel() {

        if (!this.collapsibleMenu.isEmpty()) {
            return this.collapsibleMenu;
        }

        this.init();
        for (Entry<String, String> main : mainMenu.entrySet()) {
            SimpleMenuItem mainItem = this.addEntry(this.collapsibleMenu, main);

            // Add your menu items here

            // ICEfusion standard entries
            if (main.getKey().equals("extra")) {
                for (Entry<String, String> extra : extraMenu.entrySet()) {
                    SimpleMenuItem extraItem = this.addEntry(mainItem, extra);
                }
            }
        }
        return this.collapsibleMenu;
    }


    /**
     * Resets current menu definition for locale changes.
     */
    public void resetCollapsibleMenu() {
        if (this.collapsibleMenu != null) {
            this.collapsibleMenu.clear();
        }
    }
}
